package com.itheima.day15.第一题;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Demo {
    //创建集合,维护抽奖数据
    static ArrayList<Integer> prizePool = new ArrayList<>() ;
    //定义状态量
    static boolean flag = true;
    static {
        Collections.addAll(prizePool,100, 200, 300, 400, 500, 600, 700, 800, 900, 1000);
    }
    static  Random r = new Random();
    public static void main(String[] args) {
        new Thread(() ->{
            while (true){
                synchronized ("lock"){
                    if (prizePool.size() == 0){
                        break;   //抽完退出
                    }else {
                        if (flag){
                            System.out.println("A抽出奖金" + prizePool.remove(r.nextInt(prizePool.size())));
                            flag = false;   //变更状态
                            "lock".notify();    //唤醒b线程
                        }else{
                            try {
                                "lock".wait();  //等待b线程
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }

            }
        }).start();

        new Thread(() ->{
            while (true){
                synchronized ("lock"){
                    if (prizePool.size() == 0){
                        break;   //抽完退出
                    }else {
                        if (!flag){
                            System.out.println("B抽出奖金" + prizePool.remove(r.nextInt(prizePool.size())));
                           flag = true;
                            "lock".notify();
                        }else{
                            try {
                                "lock".wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }

            }
        }).start();
    }

}
